﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class MEMElement:NmonStatElement
    {
        protected new ILog _log=LogManager.GetLogger(typeof(MEMElement));
        protected IDataParameter _zzzz = null;
        protected IDataParameter _realfree = null;
        protected IDataParameter _virtualfree = null;
        protected IDataParameter _realtotal = null;
        protected IDataParameter _virtualtotal = null;
        public string Zzzz {get;protected set;}
        public float RealFree{get;protected set;}
        public float VirtualFree { get; protected set; }
        public float RealTotal { get; protected set; }
        public float VirtualTotal {get; protected set;}
        public MEMElement(IDbConnection connection)
        {
            Balise = BaliseType.MEM;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq="INSERT INTO "+NmonTables.TablesDictionnary.Instance["MEM"]+" ([zzzz],[Realfree],[Virtualfree],[Realtotal],[Virtualtotal]) VALUES (@zzzz,@Realfree,@Virtualfree,@Realtotal,@Virtualtotal)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _realfree = Command.CreateParameter();
            _realfree.ParameterName = "@Realfree";
            _realfree.DbType = DbType.Decimal;
            _virtualfree = Command.CreateParameter();
            _virtualfree.ParameterName = "@Virtualfree";
            _virtualfree.DbType = DbType.Decimal;
            _realtotal = Command.CreateParameter();
            _realtotal.ParameterName = "@Realtotal";
            _realtotal.DbType = DbType.Decimal;
            _virtualtotal = Command.CreateParameter();
            _virtualtotal.ParameterName = "@Virtualtotal";
            _virtualtotal.DbType = DbType.Decimal;
        }
        public MEMElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _realfree.Value = RealFree;
                _virtualfree.Value = VirtualFree;
                _realtotal.Value = RealTotal;
                _virtualtotal.Value = VirtualTotal;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_realfree);
                Command.Parameters.Add(_virtualfree);
                Command.Parameters.Add(_realtotal);
                Command.Parameters.Add(_virtualtotal);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith( "MEM"))
                {
                    _log.Error("Not MEM Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Mem"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                float res = -1;
                if(float.TryParse(elements[2],NumberStyles.Float,CultureInfo.InvariantCulture ,out res))
                {
                    RealFree = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    VirtualFree = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    RealTotal = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    VirtualTotal = res;

                }
                
                                
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
